/**
 * cyw.com Inc.
 * Copyright (c) 2014-2015 All Rights Reserved.
 */
package com.rainbow.bam.entity;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.springframework.util.CollectionUtils;

import com.rainbow.bam.util.StringUtil;

/**
 * 账户信息实体类
 * 
 * @author HarrisonHan
 * @version $Id: HtAccount.java, v 0.1 2015年10月26日 上午9:51:52 HarrisonHan Exp $
 */
@Entity
@Table(name = "ht_account")
@NamedQuery(name = "HtAccount.findAll", query = "SELECT c FROM HtAccount c")
public class HtAccount implements Serializable {
    /**  */
    private static final long serialVersionUID = -6612153566082678820L;
    /**
     * 账户主键id
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(unique = true, nullable = false)
    private Long              id;
    /**
     * 账户编码
     */
    @Column(name = "account_key", nullable = false, length = 23)
    private String            accountKey;
    /**
     * 账户名称
     */
    @Column(name = "account_name", nullable = false, length = 20)
    private String            accountName;
    /**
     * 密码
     */
    @Column(name = "password", nullable = false, length = 100)
    private String            password;
    /**
     * 账户状态
     * 状态 : 0 无效 1 有效
     */
    @Column(name = "account_status", nullable = false)
    private int               accountStatus;
    /**
     * 是否激活
     * 状态 : 0 未激活 1已激活
     */
    @Column(name = "is_active", nullable = false)
    private int               isActive;
    /**
     * 用户名称
     */
    @Column(name = "user_name", nullable = false, length = 10)
    private String            userName;
    /**
     * 邮箱
     */
    @Column(name = "email", nullable = false, length = 50)
    private String            email;
    /**
     * 电话号码
     */
    @Column(name = "telephone", nullable = true, length = 15)
    private String            telephone;
    /**
     * 性别
     */
    @Column(name = "gender", nullable = true, length = 1)
    private String              gender;

    /**
     * 创建时间
     */
    @Column(name = "create_date", nullable = false)
    private Date              createDate;
    /**
     * 修改时间
     */
    @Column(name = "modify_date", nullable = false)
    private Date              modifyDate;

    /**
     *  随机码（公盐）
     */
    @Column(name = "salt", nullable = false)
    private String salt ;
    
    /**
     *shiro认证密码 不持久化到数据库，也不作为显示
     */
    @Transient
    private String plainPassword;
    /**
     * 拥有的角色列表
     */
    @Transient
    private List<Long> roleIds; 
    
    @Column(name = "role_ids", nullable = false)
    private  String roleIdsStr;
    /**
     * 是否被锁定
     */
    private Boolean locked = Boolean.FALSE;
    
    
    
    @OneToMany(cascade={CascadeType.ALL}) 
    @JoinColumn(name = "acct_id")
    private Set<HtAccountDuty> htAccountDuties;
    
    
    

    public Set<HtAccountDuty> getHtAccountDuties() {
        return htAccountDuties;
    }

    public void setHtAccountDuties(Set<HtAccountDuty> htAccountDuties) {
        this.htAccountDuties = htAccountDuties;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getAccountKey() {
        return accountKey;
    }

    public void setAccountKey(String accountKey) {
        this.accountKey = accountKey;
    }

    public String getAccountName() {
        return accountName;
    }

    public void setAccountName(String accountName) {
        this.accountName = accountName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getAccountStatus() {
        return accountStatus;
    }

    public void setAccountStatus(int accountStatus) {
        this.accountStatus = accountStatus;
    }

    public int getIsActive() {
        return isActive;
    }

    public void setIsActive(int isActive) {
        this.isActive = isActive;
    }

    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    public Date getModifyDate() {
        return modifyDate;
    }

    public void setModifyDate(Date modifyDate) {
        this.modifyDate = modifyDate;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getSalt() {
        return salt;
    }

    public void setSalt(String salt) {
        this.salt = salt;
    }

    public String getPlainPassword() {
        return plainPassword;
    }

    public void setPlainPassword(String plainPassword) {
        this.plainPassword = plainPassword;
    }

    public List<Long> getRoleIds() {
            roleIds = new ArrayList<Long>();
        if(StringUtil.isBlank(roleIdsStr)){
            return roleIds;
        }
        String[] roleIdStrs = roleIdsStr.split(",");
        for(String roleIdStr : roleIdStrs) {
            if(StringUtil.isEmpty(roleIdStr)) {
                continue;
            }
            roleIds.add(Long.valueOf(roleIdStr));
        }
        return roleIds ;
    }
    public void setRoleIds(List<Long> roleIds) {
        this.roleIds = roleIds;
        if(!CollectionUtils.isEmpty(roleIds)) {
            StringBuilder s = new StringBuilder();
            for(Long roleId : roleIds) {
                s.append(roleId);
                s.append(",");
            }
            roleIdsStr = s.toString();
        }
    }

    public Boolean getLocked() {
        return locked;
    }

    public void setLocked(Boolean locked) {
        this.locked = locked;
    }
    
}
